<?php
/**
 * Plugin Name: Friends and Family
 * Plugin URI: 
 * Description: Only allow access to those people who know you
 * Author: Jacob Brunson
 * Version: 0.1
 * Author URI: http://jacobbrunson.com
 */


class friendsAndFamily {

    public static function compare_answers($real_answers, $users_answer) {
        $good_answers = explode(' ', $real_answers);
        foreach ($good_answers as $ga) {
            if (trim(strtolower($ga)) == trim(strtolower($users_answer))) {
                return true;
            } else {

            }
        }
        return false;
    }

    public static function check_auth_to_read () {
        if (is_user_logged_in()) { $_SESSION['friendsandfamily'] = 'yes'; return; }
        $access_granted = false;
        if (isset($_POST['ffanswer_one'])) {
            if (self::compare_answers(get_option('ff_answer_one'), $_POST['ffanswer_one'])) {
                $access_granted = true;
            } else {
                $nope = TRUE;    
            }
        }
        if (isset($_POST['ffanswer_two'])) {
            if (self::compare_answers(get_option('ff_answer_two'), $_POST['ffanswer_two'])) {
                $access_granted = true;
            } else {
                $nope = TRUE;    
            }
        }
        if (isset($_POST['ffanswer_three'])) {
            if (self::compare_answers(get_option('ff_answer_three'), $_POST['ffanswer_three'])) {
                $access_granted = true;
            } else {
                $nope = TRUE;   
            }
        }
        if (isset($_POST['rememberme']) && $_POST['rememberme'] == 'forever') {
            $expire = time()+60*60*24*30*1000;
        } else {
            $expire = 0;   
        }
        if ($access_granted) {
            if (isset($_POST['rememberme'])) {
                $expires = time()+60*60*24*365*3;
            } else {
                $expires = 0;
            }
            setcookie('friendsonly2', 'yes', $expires, '/', '.brunsonfamily.us');
            $_SESSION['friendsandfamily'] = 'yes';
            return;    
        }
        if ( !isset($_SESSION['friendsandfamily']) && !isset($_COOKIE['friendsonly2']) && ((strpos($_SERVER['PHP_SELF'], 'wp-login.php') === false) && (strpos($_SERVER['PHP_SELF'], 'wp-register.php') === false) && (strpos($_SERVER['PHP_SELF'], 'wp-admin') === false)) ) {
            include(dirname(__FILE__) .'/loginpage.php');
            die();
        } else {
            $_SESSION['friendsandfamily'] = 'yes';
        }
    }
    
    function settings_page() {

        if (!current_user_can('manage_options')) {
            wp_die( __('You do not have sufficient permissions to access this page.') );
        }

        ?>
        <div class="wrap">
        <h2>Friends and Family</h2>
        
        <form method="post" action="options.php">
            <?php settings_fields( 'friendsandfamily-settings-group' ); ?>
            <table class="form-table" border="1">
                <tr valign="top">
                <th scope="row">Page Title</th>
                <td><input type="text" name="ff_page_title" value="<?php echo get_option('ff_page_title'); ?>"  size="40"/></td>
                </tr>
                 
                <tr valign="top">
                <th scope="row">Question 1</th>
                <td><input type="text" name="ff_question_one" value="<?php echo get_option('ff_question_one'); ?>" size="40"/></td>
                </tr>
                
                <tr valign="top">
                <th scope="row">Answer 1</th>
                <td><input type="text" name="ff_answer_one" value="<?php echo get_option('ff_answer_one'); ?>"  size="40"/></td>
                </tr>

                <tr valign="top">
                <th scope="row">Question 2</th>
                <td><input type="text" name="ff_question_two" value="<?php echo get_option('ff_question_two'); ?>" size="40"/></td>
                </tr>
                
                <tr valign="top">
                <th scope="row">Answer 2</th>
                <td><input type="text" name="ff_answer_two" value="<?php echo get_option('ff_answer_two'); ?>"  size="40"/></td>
                </tr>

                <tr valign="top">
                <th scope="row">Question 3</th>
                <td><input type="text" name="ff_question_three" value="<?php echo get_option('ff_question_three'); ?>" size="40"/></td>
                </tr>
                
                <tr valign="top">
                <th scope="row">Answer 3</th>
                <td><input type="text" name="ff_answer_three" value="<?php echo get_option('ff_answer_three'); ?>"  size="40"/></td>
                </tr>
            </table>
            
            <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
            </p>
        
        </form>
        </div>
        <?php 
    }


    function register_mysettings() {
        //register our settings
        register_setting( 'friendsandfamily-settings-group', 'ff_page_title' );
        register_setting( 'friendsandfamily-settings-group', 'ff_question_one' );
        register_setting( 'friendsandfamily-settings-group', 'ff_answer_one' );
        register_setting( 'friendsandfamily-settings-group', 'ff_question_two' );
        register_setting( 'friendsandfamily-settings-group', 'ff_answer_two' );
        register_setting( 'friendsandfamily-settings-group', 'ff_question_three' );
        register_setting( 'friendsandfamily-settings-group', 'ff_answer_three' );
    }


    function create_menu() {

        //create new top-level menu
        //add_menu_page('Settings', 'Friends and Family Settings', 'administrator', __FILE__, array('friendsAndFamily', 'settings_page'), plugins_url('/images/icon.png', __FILE__));
        //add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function )
        //call register settings function
        add_action( 'admin_init', array('friendsAndFamily', 'register_mysettings') );
        add_options_page('Friends and Family Options', 'Friends and Family', 'manage_options', 'ff-options-page', array('friendsAndFamily', 'settings_page'));
}


}

/**
 * Add filters and actions
 */
add_filter('init', array('friendsAndFamily','check_auth_to_read'));
add_action('admin_menu', array('friendsAndFamily', 'create_menu'));

